package com.houtai.service.activity;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TdActivity;
import com.houtai.bean.dto.activity.PageActivity;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TdActivityDao;
import com.houtai.dao.TsJzAdminOperationDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class ActivityService {
    private static Logger logger = Logger.getLogger(ActivityService.class);
    @Autowired
    private TdActivityDao tdActivityDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    /**
     * @Purpose   查找活动
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findActivityPage(PageActivity page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findActivityPage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "Activity_id desc");
            List<TdActivity> lists = tdActivityDao.findTdActivityList(page);
            PageInfo<TdActivity> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("activity_name", page.getActivity_name());
            map.put("activity_type", page.getActivity_type());
            map.put("state", page.getState());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找活动" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   添加活动
     * @param     tdActivity
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo insertTdActivity(TdActivity tdActivity, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTdActivity:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdActivity.setStart_time(format.parse(tdActivity.getDatemin()));
            tdActivity.setEnd_time(format.parse(tdActivity.getDatemax()));
            tdActivityDao.insertTdActivity(tdActivity);
            retInfo.setMark("0");
            retInfo.setTip("创建成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加活动,id:" + tdActivity.getActivity_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改活动
     * @param     tdActivity
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTdActivity(TdActivity tdActivity, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTdActivity:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdActivity.setStart_time(format.parse(tdActivity.getDatemin()));
            tdActivity.setEnd_time(format.parse(tdActivity.getDatemax()));
            tdActivityDao.updateTdActivity(tdActivity);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改活动,id:" + tdActivity.getActivity_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   删除活动
     * @param     activity_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo deleteTdActivity(int activity_id, int state, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":deleteTdActivity:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tdActivityDao.updateTdActivityState(activity_id, state);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改活动状态,id:" + activity_id
                     + "状态：" + state,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TdActivity findTdActivityById(Integer activity_id) {
        return tdActivityDao.findTdActivityDetail(activity_id);
    }

}
